﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Wandering
{
    public partial class MainForm : Form
    {


        public MainForm()
        {
            InitializeComponent();

        }

        public static MainForm MyProperty { get; set; }


        private void button1_Click(object sender, EventArgs e)     //添加
        {
            Thes thes = new Thes();
            thes.Show();
              
        }

        private void button2_Click(object sender, EventArgs e)     //删除
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                var id = dataGridView1.SelectedCells[0].Value.ToString();

                var len = MessageBox.Show("确定要删除当前选中的行吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                if (DialogResult.OK == len)
                {

                    string sql = string.Format("delete Articles where id={0}", id);

                    var b = DBHelper.InsertOrDeleteOrUpdate(sql);


                    if (b >= 1)
                    {
                        MessageBox.Show("删除成功！！");

                        string sqlses= "select * from Articles";

                        dataGridView1.DataSource = DBHelper.GetDataTable(sqlses);
                    }
                }

            }
        }
        private void button3_Click(object sender, EventArgs e)     //更新
        {

            Form2 form2 = new Form2();
            form2.Show();
        }

        private void button4_Click(object sender, EventArgs e)     //查询
        {
            var sql = string.Format("select * from Articles where Title like '%{0}%'", txtTitle.Text);

            dataGridView1.DataSource = DBHelper.GetDataTable(sql);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            MyProperty = this;

            string sql = "select * from Articles";

            dataGridView1.DataSource = DBHelper.GetDataTable(sql);

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.AllowUserToAddRows = false;

        }
    }
}
